package com.jiefeng.manage.web.controller;

import com.jiefeng.manager.queryRequest.MerchantRequest;
import com.jiefeng.manager.service.MerchantService;
import com.jiefeng.manager.table.MerchantInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hts on 2016/6/26.
 */
@Controller
public class MerchantCtrl {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private MerchantService merchantService;

    /*
    * 商户列表页面
    * */
    @RequestMapping(value = "/merchant/list")
    public String list(@ModelAttribute("pageParam") MerchantRequest merchantRequest) {
        //分页
        int pageStartNum = 0;
        //分页计算
        pageStartNum = (merchantRequest.getPageNum() - 1) * merchantRequest.getPageSize();
        String page = " limit " + String.valueOf(pageStartNum) + "," + String.valueOf(merchantRequest.getPageSize());
        merchantRequest.setPage(page);
        try {
            List<MerchantInfo> merchantList = merchantService.getListByPage(merchantRequest);
            merchantRequest.setTotalCount(merchantService.getCount(merchantRequest));
            request.setAttribute("pageParam", merchantRequest);
            request.setAttribute("merchantList", merchantList);
        } catch (java.lang.Exception exception) {
            exception.printStackTrace();
        }
        return "/jiefeng/pay/merchantList";
    }


    /*
    *开始新增商户
    *跳转到新增商户页面
    */
    @RequestMapping(value = "/merchant/addBefore")
    private String addMerchantBefore(){
        return "/jiefeng/pay/merchantAdd";
    }


    /**
     * ajax
     */
    @RequestMapping(value = "/merchant/add")
    @ResponseBody
    public Map<String, String> addMerchant(@ModelAttribute("property") MerchantInfo merchantInfo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String message = "";
        boolean result = false;
        try {
            merchantService.addMerchant(merchantInfo);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("statusCode", "300");
            message = "操作失败";
        }
        if (result) {
            resultMap.put("statusCode", "200");
            message = "操作成功";
        }
        resultMap.put("message", message);
        resultMap.put("callbackType", "closeCurrent");
        return resultMap;
    }

    /**
    *开始编辑商户
    *跳转到新增商户页面
    */
    @RequestMapping(value = "/merchant/updateBefore/{merchantId}")
    public String updateMerchantBefore(@PathVariable("merchantId") String merchantId){
        MerchantInfo merchantInfo = merchantService.getMerchantById(merchantId);
        request.setAttribute("merchantInfo", merchantInfo);
        return "/jiefeng/pay/merchantUpdate";
    }

    /**
     * 修改商户
     * ajax
     */
    @RequestMapping(value = "/merchant/update")
    @ResponseBody
    public Map<String, String> updateMerchant(@ModelAttribute("property") MerchantInfo merchantInfo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String message = "";
        boolean result = false;
        try {
            merchantService.updateMerchant(merchantInfo);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("statusCode", "300");
            message = "操作失败";
        }
        if (result) {
            resultMap.put("statusCode", "200");
            message = "操作成功";
        }
        resultMap.put("message", message);
        resultMap.put("callbackType", "closeCurrent");
        return resultMap;
    }


    @RequestMapping(value = "/merchant/delete/{merchantId}")
    @ResponseBody
    public Map<String, String> delete(@PathVariable("merchantId") String merchantId) {
        Map<String, String> paraMap = new HashMap<String, String>();
        String message = "";
        boolean result = false;
        try {
            merchantService.deleteMerchant(merchantId);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            paraMap.put("statusCode", "300");
            message = "操作失败";
        }
        if (result) {
            paraMap.put("statusCode", "200");
            message = "操作成功";
        }
        paraMap.put("message", message);
        paraMap.put("callbackType", "refreshCurrent");
        return paraMap;
    }


}
